

/**
 *\*\file log.c
 *\*\author Nations
 *\*\version v1.0.0
 *\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 **/
#include "string.h"
#include "http_connect.h"
#include "ring_buffer.h"
#include "rtthread.h"
#include "json_options.h"
#include "DefultParameter.h"


/**
 * @brief
 *
 */
void http_connect_init(void)
{
}
/**
 * @brief
 *
 * @param buffer
 * @param length
 * @return int
 */
int http_requst_encode(void *buffer, int length)
{
    int retlength = -1;

    Jsonoptions_handler json, *hand;
    hand = &json;
    json_handler_defult(hand);
    json._PACK_json_cache_size = 512;
    json._packJsonMem = rt_malloc(json._PACK_json_cache_size);

    _ALL_PARA *PARA = sys_para_get_all();

    jsonPack root;
    createJsonPack(hand, &root);
    do
    {
        if (0 != addJsonStrPair(hand, &root, KEY_METHOD, "dtu.connect"))
            break;
        if (0 != addJsonStrPair(hand, &root, KEY_SN, PARA->SN))
            break;
        if (0 != addJsonStrPair(hand, &root, KEY_VER, PROTCOL_VER))
            break;
        jsonPack bizcontent;
        if (0 != addJsonObject(hand, &root, KEY_BIZCONTENT, &bizcontent))
            break;
        if (0 != addJsonStrPair(hand, &bizcontent, KEY_FWVER, FwVer))
            break;
        char *str = getPackJsonString(hand);
        retlength = rt_strnlen(str, 512);
        if (retlength > 0)
            memcpy(buffer, str, retlength);
        else
            retlength = -1;
    } while (0);
    rt_free(json._packJsonMem);
    return retlength;
}

/**
 * @brief
 *
 * @param para
 * @param data
 * @param length
 * @return int
 */
int http_connect_decode(Devcie_Dtu_Link_Para *para, char *data, int length)
{
#define decode_err                            \
    {                                         \
        /*rt_kprintf("err %d\n", __LINE__);*/ \
        break;                                \
    }

    Jsonoptions_handler json, *hand;
    _ALL_PARA *PARA = sys_para_get_all();
    char *tempstr = NULL;
    int tempvalue = 0;
    int arry_count = 0;
    int retsta = -1;
    hand = &json;
    json_handler_defult(&json);
    json._JSON_parse_node_count = 30;
    json._jsonParseNodeList = rt_malloc(sizeof(jsonObj) * json._JSON_parse_node_count);

    do
    {
        // 解析json
        jsonObj *root = jsonParse(hand, data);
        if (root == NULL)
            decode_err;
        // 验证method
        tempstr = getJsonObjString(hand, root, KEY_METHOD);
        if (tempstr == NULL)
            decode_err;
        if (RT_NULL == rt_strstr(tempstr, "dtu.connect.resp"))
            decode_err;
        // 验证SN码
        tempstr = getJsonObjString(hand, root, KEY_SN);
        if (tempstr == NULL)
            decode_err;
        if (RT_NULL == rt_strstr(tempstr, PARA->SN))
            decode_err;
        // 验证返回值
        tempvalue = getJsonObjInteger(hand, root, KEY_CODE);
        if (tempvalue != 0)
            decode_err;
        // 获取回执消息
        jsonObj *biz = jsonObject(hand, root, KEY_BIZCONTENT);
        if (biz == RT_NULL)
            decode_err;
        // 获取连接参数
        jsonObj *unit = jsonObject(hand, biz, KEY_CONNECTION);
        if (unit == RT_NULL)
        {
            decode_err;
        }
        // 获取目标服务器
        tempstr = getJsonObjString(hand, unit, KEY_HOST);
        if (tempstr == NULL)
            decode_err;
        memset(para->server_host, 0, sizeof(para->server_host));
        memcpy(para->server_host, tempstr, strlen(tempstr));
        // 获取目标端口
        tempvalue = getJsonObjInteger(hand, unit, KEY_PORT);
        if (tempvalue <= 0)
            decode_err;
        para->server_port = tempvalue;
        // 获取mqtt keepalive
        tempvalue = getJsonObjInteger(hand, unit, KEY_KEEPALIVE);
        if (tempvalue <= 0)
            decode_err;
        para->mqtt_keepalive = tempvalue;
        // 获取clientid
        tempstr = getJsonObjString(hand, unit, KEY_CLIENTID);
        if (tempstr == RT_NULL)
            decode_err;
        memset(para->mqtt_clientid, 0, sizeof(para->mqtt_clientid));
        memcpy(para->mqtt_clientid, tempstr, strlen(tempstr));
        // 获取username
        tempstr = getJsonObjString(hand, unit, KEY_USERNAME);
        if (tempstr == RT_NULL)
            decode_err;
        memset(para->mqtt_username, 0, sizeof(para->mqtt_username));
        memcpy(para->mqtt_username, tempstr, strlen(tempstr));
        // 获取password
        tempstr = getJsonObjString(hand, unit, KEY_PASSWORD);
        if (tempstr == RT_NULL)
            decode_err;
        memset(para->mqtt_password, 0, sizeof(para->mqtt_password));
        memcpy(para->mqtt_password, tempstr, strlen(tempstr));

        jsonRelease(hand, unit);

        // topic部分
        unit = jsonArray(hand, biz, KEY_TOPIC);
        if (unit == RT_NULL)
        {
            decode_err;
        }
        arry_count = (unit->count > 0) ? 1 : 0; // 目前只有一组topic，因此此处做限制
        int i = 0;
        for (i = 0; i < arry_count; i++)
        {
            // 订阅主题解析
            jsonObj *arrunit = getJsonArrObject(hand, unit, i);
            jsonObj *topic = jsonObject(hand, arrunit, KEY_SUB);
            if (topic == RT_NULL)
            {
                decode_err;
            }
            else
            {
                tempvalue = getJsonObjInteger(hand, topic, KEY_QOS);
                if (tempvalue < 0 || tempvalue > 2)
                    decode_err;
                para->mqtt_subqos = tempvalue;

                tempstr = getJsonObjString(hand, topic, KEY_TOPIC);
                memset(&para->mqtt_topic_subcribe, 0, sizeof(para->mqtt_topic_subcribe));
                memcpy(&para->mqtt_topic_subcribe, tempstr, strlen(tempstr));
                jsonRelease(hand, topic);
            }
            // 发布主体解析
            topic = jsonObject(hand, arrunit, KEY_PUB);
            if (topic == RT_NULL)
            {
                decode_err;
            }
            else
            {
                tempvalue = getJsonObjInteger(hand, topic, KEY_QOS);
                if (tempvalue < 0 || tempvalue > 2)
                    decode_err;
                para->mqtt_pubqos = tempvalue;

                tempstr = getJsonObjString(hand, topic, KEY_TOPIC);
                memset(&para->mqtt_topic_publish, 0, sizeof(para->mqtt_topic_publish));
                memcpy(&para->mqtt_topic_publish, tempstr, strlen(tempstr));
                jsonRelease(hand, topic);
            }
            jsonRelease(hand, arrunit);
        }
        if (i != arry_count)
            decode_err;
        jsonRelease(hand, unit);
        // 串口参数部分
        unit = jsonObject(hand, biz, KEY_UART);
        if (unit == RT_NULL)
        {
            decode_err;
        }

        jsonRelease(hand, unit);
        // 数据周期
        tempvalue = getJsonObjInteger(hand, biz, KEY_DATAPERIOD);
        if (tempvalue < 0 && tempvalue > 65535)
            decode_err;
        para->dataperiod = tempvalue;

        // rt_kprintf("server_host:%s\n", para->server_host);
        // rt_kprintf("mqtt_clientid:%s\n", para->mqtt_clientid);
        // rt_kprintf("mqtt_username:%s\n", para->mqtt_username);
        // rt_kprintf("mqtt_password:%s\n", para->mqtt_password);
        // rt_kprintf("mqtt_topic_publish:%s\n", para->mqtt_topic_publish);
        // rt_kprintf("mqtt_topic_subcribe:%s\n", para->mqtt_topic_subcribe);
        // rt_kprintf("server_port:%d\n", para->server_port);

        // rt_kprintf("mqtt_keepalive:%d\n", para->mqtt_keepalive);
        // rt_kprintf("mqtt_subqos:%d\n", para->mqtt_subqos);
        // rt_kprintf("mqtt_pubqos:%d\n", para->mqtt_pubqos);
        // rt_kprintf("uart_parity:%d\n", para->uart_parity);
        // rt_kprintf("uart_stopbit:%d\n", para->uart_stopbit);
        // rt_kprintf("uart_databit:%d\n", para->uart_databit);
        // rt_kprintf("dataperiod:%d\n", para->dataperiod);
        // rt_kprintf("uart_baud:%d\n", para->uart_baud);

        retsta = 0;
    } while (0);

    rt_free(json._jsonParseNodeList);
    return retsta;
#undef decode_err
}
